package com.leetcode.dp;

import java.util.List;

/**
 * @author Dennis Li
 * @date 2020/7/20 22:17
 */
public class MinimumTotal_120 {

    public int minimumTotal(List<List<Integer>> triangle) {
        int size = triangle.size();
        int[][] dp = new int[size][triangle.get(size - 1).size()];
        dp[0][0] = triangle.get(0).get(0);
        for (int i = 1; i < size; i++) {
            for (int j = 0; j < triangle.get(i).size(); j++) {
                dp[i][j] = triangle.get(i).get(j);
                if (j == 0)
                    dp[i][j] += dp[i - 1][j];
                else if (j == triangle.get(i).size() - 1)
                    dp[i][j] += dp[i - 1][j - 1];
                else
                    dp[i][j] += Math.min(dp[i - 1][j - 1], dp[i - 1][j]);
            }
        }
        int min = Integer.MAX_VALUE;
        for (int num : dp[size - 1])
            min = Math.min(min, num);
        return min;
    }

}
